package com.backstage.service.impl;

import com.backstage.dao.ChatroomMapper;
import com.backstage.pojo.Chatroom;
import com.backstage.pojo.User;
import com.backstage.service.ChatroomService;
import com.backstage.utils.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatroomServiceImpl implements ChatroomService {
    @Autowired
    private ChatroomMapper chatroomMapper;

    public int selectChatroomTotal(){
        return chatroomMapper.selectChatroomTotal();
    }

    public int selectChatroomScopeId(Integer id){
        return chatroomMapper.selectChatroomScopeId(id);
    }

    public int selectInsertNewChatroom(Chatroom record){
        return chatroomMapper.insert(record);
    }

    public List<Chatroom> selectChatroomScopeIdList(Integer scopeId, Integer userId){
        return chatroomMapper.selectChatroomScopeIdList(scopeId, userId);
    }

    public Chatroom selectChatroomGetId(Integer id){
        return chatroomMapper.selectByPrimaryKey(id);
    }

    //查询当前用户是否在 该区域，或是自己开设的聊天室
    @Override
    public Chatroom selectChatroomHas(Integer scopeId, Integer userId, Integer chatid) {
        return chatroomMapper.selectChatroomHas(scopeId,userId,chatid);
    }

    public int selectChatroomUpdate(Chatroom record){
        return chatroomMapper.updateByPrimaryKeySelective(record);
    }

    //获取聊天室列表，带搜索分页
    public PageBean<Chatroom> selectChatroomList(Integer pageNum, Integer pageSize, String keyWord){

        PageBean<Chatroom> pageBean = new PageBean<Chatroom>();
        Chatroom chatroom = new Chatroom();
        if(!"".equals(keyWord)){
            chatroom.setTitle(keyWord);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Chatroom> chatroomList = chatroomMapper.selectChatroomList(chatroom);
        PageInfo<Chatroom> goodsPageInfo = new PageInfo<Chatroom>(chatroomList);

        pageBean.setList(chatroomList);
        pageBean.setPageBeanObj(goodsPageInfo.getPageNum(),goodsPageInfo.getPageSize(),goodsPageInfo.getNextPage(),goodsPageInfo.getPrePage(),goodsPageInfo.getTotal(),goodsPageInfo.getPages());

        return pageBean;

    }
}
